package jia.tools;

import jia.game.GenericFlags;
import org.apache.log4j.Logger;
import org.dom4j.io.SAXReader;
import org.dom4j.Document;
import org.dom4j.DocumentException;

import java.io.File;
import java.net.MalformedURLException;

/**
 * @author Erik Stell
 * @version 1.0
 */
public class GnomeNameGenerator {
    private static final Logger log = Logger.getLogger(GnomeNameGenerator.class);
    private static File fileToBeParsed = new File("src/com/gryffonx/xml/gnome.xml");
    private static String gender = "";
    private static SAXReader reader = new SAXReader();
    private static Document xmlDoc = null;
    
    /**
     *
     * @param gender
     * @param howMany
     * @return
     */
    public static String generateName(String gender, int howMany) throws MalformedURLException {
        if (howMany <= 0) {
            howMany = 1;
        }
        try {
            xmlDoc = reader.read(fileToBeParsed);
        } catch (DocumentException e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        setGender(gender);
        StringBuffer retVal = new StringBuffer();
        
        for (int i = 0; i < howMany; i++) {
            StringBuffer finishedName = new StringBuffer();
            String group = determineGroup();
            
            try {
                finishedName.append(getPrefix(group));
                finishedName.append(extractSuffix());
            } catch (Exception e) {
                if (GenericFlags.errorMessages){
                    log.error(e.getMessage());
                }
            }
            
            retVal.append(StringUtils.capitalize(finishedName.toString()));
            // retVal.append("\n");
        }
        
        return retVal.toString();
    }
    
    /**
     *
     * @return
     */
    private static String determineGroup() {
        String groupName = "";
        try {
            int howMany = xmlDoc.selectNodes("//prefixElements/prefix").size();
            int rolld3 = StaticDiceRoller.rolldSomething(howMany, 1);
            groupName = new Integer(rolld3).toString();
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return groupName;
    }
    
    /**
     *
     * @param groupName
     * @return
     */
    private static String getPrefix(String groupName) {
        String retVal = "";
        try {
            retVal = StringUtils.capitalize(XMLUtils.getNodeValue("prefix", xmlDoc, Integer.parseInt(groupName)));
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal;
    }
    /**
     *
     * @return
     */
    private static String extractSuffix() {
        String retVal = "";
        try {
            retVal = XMLUtils.getNodeValue("suffix", xmlDoc, getGender().toLowerCase());
        } catch (Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
        return retVal;
    }
    
    /**
     *
     * @return
     */
    public static String getGender() {
        return gender;
    }
    
    /**
     *
     * @param gender
     */
    public static void setGender(String gender) {
        GnomeNameGenerator.gender = gender;
    }
}
